﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;
using HsTec.Mobile.Green.SqlServerDAL;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Net;

public partial class DocumentManage_Up_Doccument : System.Web.UI.Page
{
    Machineroom machineroom = new Machineroom();
    Network MyNt = new Network();
    Area area = new Area();
    Project project = new Project();
    LogManage MyLog = new LogManage();

    NetCommClass MyNetCC = new NetCommClass();

    HsTec.Mobile.Green.SqlServerDAL.DataFile MyDataFile = new DataFile();


    BaimasoftFile BaimasoftFile = new BaimasoftFile();

    string sFileExtension = ConfigurationManager.AppSettings["FileExtension"];  //获取文件扩展名

    static public ArrayList hif = new ArrayList(); //保存文件列表
    static public ArrayList hif2 = new ArrayList(); //保存文件列表

    static public ArrayList ahif = new ArrayList(); //保存别名文件列表
    public int filesUploaded = 0; //上传文件的数量

    public string sIsFile = "", sIsDocument = "", sMessage = "", sMessage2 = "";  //未归档记录,已归档记录

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (ViewState["sIsFile"] == null || ViewState["sIsFile"].ToString().CompareTo("") == 0)
            {
                ViewState["sIsFile"] = "";
            }
            if (ViewState["sIsDocument"] == null || ViewState["sIsDocument"].ToString().CompareTo("") == 0)
            {
                ViewState["sIsDocument"] = "";
            }

            if (ViewState["sIsFile2"] == null || ViewState["sIsFile2"].ToString().CompareTo("") == 0)
            {
                ViewState["sIsFile2"] = "";
            }
            if (ViewState["sIsDocument2"] == null || ViewState["sIsDocument2"].ToString().CompareTo("") == 0)
            {
                ViewState["sIsDocument2"] = "";
            }


            RadioFile.Checked = true;

            ddlRegionDataBind();
            ddlMachineDataBind();
            ddlnetworkDataBind();
            ddlProjectDataBind();
            KeepStyle();
            hif.Clear();
            hif2.Clear();
        }
    }


    /// <summary>
    /// 区域下拉框
    /// </summary>
    protected void ddlRegionDataBind()
    {
        ddlRegionid.DataSource = area .GetAreaInfos (string.Empty);
        ddlRegionid.DataBind();
        ddlRegionid.Items.Insert(0, "");
        ddlRegionid.Items[0].Value = "0";
    }

    /// <summary>
    /// 机房下拉框绑定
    /// </summary>
    protected void ddlMachineDataBind()
    {
        ddlMachineid.DataSource = machineroom .GetMachineRooms ("");
        ddlMachineid.DataBind();
        ddlMachineid.Items.Insert(0, "");
        ddlMachineid.Items[0].Value = "";

    }

    /// <summary>
    /// 网元下拉框绑定
    /// </summary>
    protected void ddlnetworkDataBind()
    {
        ddlnetwork.DataSource = MyNt.GetNetWorks ("");
        ddlnetwork.DataBind();
        ddlnetwork.Items.Insert(0, "");
        ddlnetwork.Items[0].Value = "";

    }

    /// <summary>
    /// 项目下拉框
    /// </summary>
    protected void ddlProjectDataBind()
    {
        this.ddlProject.DataSource = project .GetProjectInfos ("");
        this.ddlProject.DataBind();
        ddlProject.Items.Insert(0, "");
        ddlProject.Items[0].Value = "";

    }  

    /// <summary>
    /// 增加文件 (未用)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AddFile_Click(object sender, EventArgs e)
    {
        string sFolderID = HID.Value.ToString().Trim();           //当前文件夹ID
        if (Page.IsPostBack == true)
        {
            strStatus.Text = "";            
            string errorFile = "";
            if (RadioFile.Checked == true)  //文件
            {
                if (FindFile.HasFile)  //判断浏览文本框中有没有文件内容
                {
                    Boolean fileOK = false;
                    String fileExtension = System.IO.Path.GetExtension(FindFile.PostedFile.FileName).ToLower();
                    String[] allowedExtensions = sFileExtension.Split(',');
                    for (int j = 0; j < allowedExtensions.Length; j++)
                    {
                        string stempExtension = "." + allowedExtensions[j].ToLower();
                        if (fileExtension == stempExtension)
                        {
                            fileOK = true;
                            break;
                        }
                    }

                    if (fileOK)
                    {
                        string sTempFileName = System.IO.Path.GetFileName(FindFile.PostedFile.FileName);  //文件名称
                        //string sFilePath = System.IO.Path.GetPathRoot(FindFile.PostedFile.FileName);      // 文件父目录
                        //string sOnlyFileName = BaimasoftFile.GetMyOnlyFileName();                         //系统唯一文件名
                        //string sAFilePath = sFilePath + sOnlyFileName + fileExtension;                    //和当前文件同一个路径,不同文件名

                        //判断该文档是否已归档
                        int iback = MyDataFile.GetCountByqueryStr (" FolderID='" + sFolderID + "' and name = '" + sTempFileName + "' and Status='1' ");
                        if (iback > 0)  //文档已归档,不允许上传到该文件夹
                        {
                            //sIsDocument = sIsDocument + sTempFileName + "<br>";
                            ViewState["sIsDocument"] = ViewState["sIsDocument"].ToString().Trim() + FindFile.PostedFile.FileName + "<br>";
                        }
                        else  //不存在,或者当前文档没有归档,如果同意上传,则将覆盖掉以前得重名文件
                        {
                            //判断当前文件夹下面是否存在该文件
                            int iback2 = MyDataFile .GetCountByqueryStr (" FolderID='" + sFolderID + "' and name = '" + sTempFileName + "'  and Status<>'1' ");
                            if (iback2 > 0)
                            {
                                //sIsFile = sIsFile + sTempFileName + "<br>"; //";
                                ViewState["sIsFile"] = ViewState["sIsFile"].ToString().Trim() + FindFile.PostedFile.FileName + "<br>";
                            }
                            hif.Add(FindFile.PostedFile.FileName);//把文件完整路径添加到数组中去
                            FileList.Items.Add(FindFile.PostedFile.FileName); //获取一个待上传的文件路径
                        }
                        string sLeave = "";
                        if (ViewState["sIsDocument"].ToString ().Trim () != "")
                        {
                            sLeave = sLeave + "<font color=red>" + ViewState["sIsDocument"] + "</font>文档已归档，不允许上传！<br>";
                        }
                        if (ViewState["sIsFile"].ToString ().Trim () != "")
                        {
                            sLeave = sLeave + "<font color=red>" + ViewState["sIsFile"] + "</font>以上文档已存在，是否删除？如果不删除，系统将命名其为最新版本！<br>";
                        }

                        sMessage = sLeave;
                    }
                    else
                    {
                        errorFile = errorFile + fileExtension;
                        strStatus.Text = "<font color=red>您上传的部分文件格式" + errorFile + "不正确,系统只允许上传:" + sFileExtension + "格式文件！</font>";
                    }
                }
                else
                {
                    strStatus.Text = "<font color=red>必须要有上传的文件.</font>";
                }
            }
            else  //文件夹
            {
                strStatus.Text = "<font color=red>必须要有上传的文件夹.</font>";
            }
            KeepStyle();
        }
        else
        { }
    }

    /// <summary>
    /// 单文件上传 (未用)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void UpFile_Click(object sender, EventArgs e)
    {
        string m_Root = ConfigurationManager.AppSettings["DocumentMange"];  //取Web.Config文件中的配置信息,在配置的时候要注意该盘有定怕权限
        string sPaterID = HID.Value.ToString().Trim();           //父ID 
        string sPaterPath = HPath.Value.ToString().Trim();       //父目录

        string sKeyword = TextAreaKeyword.Value.ToString().Trim();
        string sDorder = this.Dorder.Value.ToString().Trim() == null ? "0" : this.Dorder.Value.ToString().Trim();
        string sMachine = ddlMachineid.SelectedValue.ToString().Trim();
        string sNetwork = ddlnetwork.SelectedValue.ToString().Trim();
        string sRegionid = ddlRegionid.SelectedValue.ToString().Trim();  //区域
        string sProject = ddlProject.SelectedValue.ToString().Trim();   //项目

        if (sDorder.Trim() == "")
        {
            sDorder = "0";
        }

        int icount = 0;
        //遍历File表单元素
        HttpFileCollection files = HttpContext.Current.Request.Files;
        if (files.Count <= 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择需要上传的文件！');</script>");
            return;
        }

        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                ///'检查文件扩展名字
                HttpPostedFile postedFile = files[iFile];
                string fileName, fileExtension;
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                if (fileName != "")
                {
                    fileExtension = System.IO.Path.GetExtension(fileName);

                    string InFile, OutFile;
                    InFile = m_Root + HPath.Value + "\\" + fileName + ".Encrypt";
                    string path = sPaterPath + "\\" + fileName; //当前的父路径

                    DataFile datafile = new DataFile();
                    //int iback = datafile.GetDataFileInfos(" a.name = '" + fileName + "' and a.FolderID = '" + sPaterID + "' ").Rows.Count;
                    //if (iback < 1)
                    //{

                    //保存到服务器指定路径
                    postedFile.SaveAs(InFile);
                    //加密
                    OutFile = InFile.Remove(InFile.Trim().Length - 8, 8);
                    string sEncryptFile = ConfigurationManager.AppSettings["EncryptFile"];

                    BaimasoftFile.EncryptFile(InFile, OutFile, sEncryptFile);//对文件进行加密处理
                    BaimasoftFile.DeleteFile(InFile);            //删除临时文件

                    //删除同一文件夹中,重复的文件(实现覆盖原文件的功能)
                    MyDataFile.DeleteDataFileInfo(" name = '" + fileName + "' and FolderID = '" + sPaterID + "' ");
                    //插入文档信息到数据库中

                    icount++;
                    //}
                    //else
                    //{
                    //}
                }
            }
            //strStatus.Text = strMsg.ToString();
            if (icount > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传成功！');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('没有上传文件！');window.close();</script>");
                return;
            }
        }
        catch (System.Exception Ex)
        {
            strStatus.Text = Ex.Message;
        }
    }

    
    /// <summary>
    /// 删除选项
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DelFile_Click(object sender, EventArgs e)
    {
        if (FileList.SelectedIndex == -1)
        {
            strStatus.Text = "<font color=red>必须指定要删除的文件.</font>";
            return;
        }
        else if (FileList.Items.Count != 0)
        {
            hif.RemoveAt(FileList.SelectedIndex);
            string sTemp = FileList.SelectedValue.ToString().Trim() + "<br>";
            FileList.Items.Remove(FileList.SelectedItem.Text);

            string sLeave = "";

            ViewState["sIsFile"] = ViewState["sIsFile"].ToString().Trim().Replace(sTemp, "");

            if (ViewState["sIsDocument"].ToString().Trim() != "")
            {
                sLeave = sLeave + "<font color=red>" + ViewState["sIsDocument"] + "</font>文档已归档，不允许上传！<br>";
            }
            if (ViewState["sIsFile"].ToString().Trim() != "")
            {
                sLeave = sLeave + "<font color=red>" + ViewState["sIsFile"] + "</font>以上文档已存在，是否删除？如果不删除，系统将命名其为最新版本！<br>";
            }

            sMessage = sLeave ;

            strStatus.Text = "";
        }
    }


   
    /// <summary>
    /// 保存相应的状态
    /// </summary>
    protected void KeepStyle()
    {
        if (RadioFile.Checked == true)
        {
            DivFile.Style.Add("display", "");
            DivFolder.Style.Add("display", "none");
        }
        else
        {
            DivFile.Style.Add("display", "none");
            DivFolder.Style.Add("display", "");
        }
    }

    /// <summary>
    /// 清空选项
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ClearFile_Click(object sender, EventArgs e)
    {
        FileList.Items.Clear();
        hif.Clear();
        strStatus.Text = "";

        ViewState["sIsFile"] = "";
        ViewState["sIsDocument"]="";
    }

    protected void ddlRegionid_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sddlRegion = ddlRegionid.SelectedValue.ToString().Trim();

        DataTable dt1 = machineroom.GetMachineRooms ("a.region_id='" + sddlRegion + "'");
        this.ddlMachineid.DataValueField = "mroom_id";
        this.ddlMachineid.DataTextField = "mroom_name";
        this.ddlMachineid.DataSource = dt1;
        this.ddlMachineid.DataBind();

        string sddlMachine = ddlMachineid.SelectedValue.ToString().Trim();
        DataTable dt2 = MyNt.GetNetWorks ("a.mroom_id = '" + sddlMachine + "'");
        this.ddlnetwork.DataValueField = "netw_id";
        this.ddlnetwork.DataTextField = "netw_name";
        this.ddlnetwork.DataSource = dt2;
        this.ddlnetwork.DataBind();
    }

    protected void ddlMachineid_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sddlMachine = ddlMachineid.SelectedValue.ToString().Trim();
        DataTable dt2 = MyNt.GetNetWorks ("a.mroom_id = '" + sddlMachine + "'");
        this.ddlnetwork.DataValueField = "netw_id";
        this.ddlnetwork.DataTextField = "netw_name";
        this.ddlnetwork.DataSource = dt2;
        this.ddlnetwork.DataBind();
    }
    
    
    /// <summary>
    /// 清空文件列表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ClearChioceFile_Click(object sender, EventArgs e)
    {
        FileList2.Items.Clear();
        hif2.Clear();
        ListStr.Text = "";

        ViewState["sIsFile2"] = "";
        ViewState["sIsDocument2"] = "";

    }

    /// <summary>
    /// 删除选择文件列表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DelChoiceFile_Click(object sender, EventArgs e)
    {
        if (FileList2.SelectedIndex == -1)
        {
            ListStr.Text = "<font color=red>必须指定要删除的文件.</font>";
            return;
        }
        else if (FileList2.Items.Count != 0)
        {
            hif2.RemoveAt(FileList2.SelectedIndex);
            string sTemp = FileList2.SelectedValue.ToString().Trim() + "<br>";
            FileList2.Items.Remove(FileList2.SelectedItem.Text);

            string sLeave = "";

            ViewState["sIsFile2"] = ViewState["sIsFile2"].ToString().Trim().Replace(sTemp, "");

            if (ViewState["sIsDocument2"].ToString().Trim() != "")
            {
                sLeave = sLeave + "<font color=red>" + ViewState["sIsDocument2"] + "</font>文档已归档，不允许上传！<br>";
            }
            if (ViewState["sIsFile2"].ToString().Trim() != "")
            {
                sLeave = sLeave + "<font color=red>" + ViewState["sIsFile2"] + "</font>以上文档已存在，是否删除？如果不删除，系统将命名其为最新版本！<br>";
            }

            sMessage2 = sLeave;

            ListStr.Text = "";
        }

    }

    /// <summary>
    /// 增加选择文件(单文件选择)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ChioceFile_Click(object sender, EventArgs e)
    {
        string sFolderID = HID.Value.ToString().Trim();           //当前文件夹ID
        if (Page.IsPostBack == true)
        {
            ListStr.Text = "";
            string errorFile = "";
            if (RadioFile.Checked == true)  //文件
            {
                if (FindFile.HasFile)  //判断浏览文本框中有没有文件内容
                {
                    Boolean fileOK = false;
                    String fileExtension = System.IO.Path.GetExtension(FindFile.PostedFile.FileName).ToLower();
                    String[] allowedExtensions = sFileExtension.Split(',');
                    for (int j = 0; j < allowedExtensions.Length; j++)
                    {
                        string stempExtension = "." + allowedExtensions[j].ToLower();
                        if (fileExtension == stempExtension)
                        {
                            fileOK = true;
                            break;
                        }
                    }

                    if (fileOK)
                    {
                        string sTempFileName = System.IO.Path.GetFileName(FindFile.PostedFile.FileName);  //文件名称

                        //判断该文档是否已归档
                        int iback = MyDataFile.GetCountByqueryStr (" FolderID='" + sFolderID + "' and name = '" + sTempFileName + "' and Status='1' ");
                        if (iback > 0)  //文档已归档,不允许上传到该文件夹
                        {
                            ViewState["sIsDocument2"] = ViewState["sIsDocument2"].ToString().Trim() + FindFile.PostedFile.FileName + "<br>";
                        }
                        else  //不存在,或者当前文档没有归档,如果同意上传,则将覆盖掉以前得重名文件
                        {
                            //判断当前文件夹下面是否存在该文件
                            int iback2 = MyDataFile.GetCountByqueryStr(" FolderID='" + sFolderID + "' and name = '" + sTempFileName + "'  and Status<>'1' ");
                            if (iback2 > 0)
                            {
                                ViewState["sIsFile2"] = ViewState["sIsFile2"].ToString().Trim() + FindFile.PostedFile.FileName + "<br>";
                            }
                            hif2.Add(FindFile);//把文件完整路径添加到数组中去
                            FileList2.Items.Add(FindFile.PostedFile.FileName); //获取一个待上传的文件路径
                        }
                        string sLeave = "";
                        if (ViewState["sIsDocument2"].ToString().Trim() != "")
                        {
                            sLeave = sLeave + "<font color=red>" + ViewState["sIsDocument2"] + "</font>文档已归档，不允许上传！<br>";
                        }
                        if (ViewState["sIsFile2"].ToString().Trim() != "")
                        {
                            sLeave = sLeave + "<font color=red>" + ViewState["sIsFile2"] + "</font>以上文档已存在，是否删除？如果不删除，系统将命名其为最新版本！<br>";
                        }

                        sMessage2 = sLeave;
                    }
                    else
                    {
                        errorFile = errorFile + fileExtension;
                        ListStr.Text = "<font color=red>您上传的部分文件格式" + errorFile + "不正确,系统只允许上传:" + sFileExtension + "格式文件！</font>";
                    }
                }
                else
                {
                    ListStr.Text = "<font color=red>必须要有上传的文件.</font>";
                }
            }
            else  //文件夹
            {
                ListStr.Text = "<font color=red>必须要有上传的文件夹.</font>";
            }
            KeepStyle();
        }
        else
        { }

    }


    /// <summary>
    /// 文件上传(单个文件)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void UploadFile_Click(object sender, EventArgs e)
    {
        string m_Root = ConfigurationManager.AppSettings["DocumentMange"];  //取Web.Config文件中的配置信息,在配置的时候要注意该盘有定怕权限
        string sPaterID = HID.Value.ToString().Trim();           //父ID 
        string sPaterPath = HPath.Value.ToString().Trim();       //父目录

        string sKeyword = TextAreaKeyword.Value.ToString().Trim();
        string sDorder = this.Dorder.Value.ToString().Trim() == null ? "0" : this.Dorder.Value.ToString().Trim();
        string sMachine = ddlMachineid.SelectedValue.ToString().Trim();
        string sNetwork = ddlnetwork.SelectedValue.ToString().Trim();
        string sRegionid = ddlRegionid.SelectedValue.ToString().Trim();  //区域
        string sProject = ddlProject.SelectedValue.ToString().Trim();   //项目

        if (sDorder.Trim() == "")
        {
            sDorder = "0";
        }

        int icount = 0;
        //遍历File表单元素
        HttpFileCollection files = HttpContext.Current.Request.Files;

        if (hif2.Count <= 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择需要上传的文件！');</script>");
            return;
        }

        foreach (System.Web.UI.WebControls.FileUpload HIF in hif2)
        {
            try
            {
                ///'检查文件扩展名字
                string fileName = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
                string sFExtension = System.IO.Path.GetExtension(HIF.PostedFile.FileName);
                if (fileName != "")
                {
                    string sRndCode = NetCommClass.GetRandomCode(8);             //系统自动生成指定数目的数字
                    string sTempFileName = sRndCode + sFExtension;
                    string InFile, OutFile;
                    InFile = m_Root + HPath.Value + "\\" + sTempFileName + ".Encrypt";  //加密文件路径
                    string path = sPaterPath + "\\" + sTempFileName;                    //当前的父路径

                    string sSaveFile = m_Root + HPath.Value + "\\" + fileName;

                    string sExtension = "";
                    string[] aExtension = sFExtension.Split('.');
                    if (aExtension[1].ToString().Trim() != "")
                    {
                        sExtension = aExtension[1].ToString().Trim().ToLower ();
                    }

                    DataFile datafile = new DataFile();

                    //保存到服务器指定路径
                    HIF.PostedFile.SaveAs(InFile);
                    //加密
                    OutFile = InFile.Remove(InFile.Trim().Length - 8, 8);
                    string sEncryptFile = ConfigurationManager.AppSettings["EncryptFile"];

                    BaimasoftFile.EncryptFile(InFile, OutFile, sEncryptFile);     //对文件进行加密处理
                    BaimasoftFile.DeleteFile(InFile);                             //删除临时文件

                    //删除同一文件夹中,重复的文件(实现覆盖原文件的功能)
                    MyDataFile.DeleteDataFileInfo(" name = '" + sTempFileName + "' and FolderID = '" + sPaterID + "' ");
                    //插入文档信息到数据库中
                    MyDataFile.InsDataFileInfo(sPaterID, fileName, sTempFileName, sKeyword, HIF.PostedFile.ContentLength.ToString().Trim(), HIF.PostedFile.ContentType.ToString().Trim(), sExtension, sDorder, "0", sRegionid, sMachine, sNetwork, sProject, Session["Baimasoft_User_LogonID"].ToString(), DateTime.Now.ToString().Trim(), "0", "", DateTime.Now.ToString().Trim(), "");

                    //日志
                    try
                    {
                        string sPerID = (string)Session["Baimasoft_Pers_ID"];
                        string sContent = "用户" + (string)Session["Baimasoft_User_LogonID"] + "上传了文件：" + sSaveFile;
                        string sIP = Page.Request.UserHostAddress;
                        MyLog.InsertLog("3", sPerID, sContent, sIP);
                    }
                    catch
                    {
                        string sPerID = "0";
                        string sContent = "上传了文件：" + sSaveFile;
                        string sIP = Page.Request.UserHostAddress;
                        MyLog.InsertLog("3", sPerID, sContent, sIP);
                    }
                    icount++;
                }
            }
            catch (System.Exception Ex)
            {
                strStatus.Text = Ex.Message;
            }
        }

        string sAlter = "";
        int ifile = hif2.Count;
        int ileave = ifile - icount;
        if (ifile == icount)
        {
            sAlter = "文档上传成功！";
        }
        else if (ifile > icount && icount > 0)
        {
            sAlter = "上传成功" + icount + "文档，还有" + ileave + "文档上传失败！";
        }
        else
        {
            sAlter = "上传文档失败！";
        }
        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + sAlter + "');window.close();</script>");
        return;
    }


    /// <summary>
    /// 增加文件夹
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AddFolder_Click(object sender, EventArgs e)
    {
        string sFolderID = HID.Value.ToString().Trim();           //当前文件夹ID

        string errorFile = "";
        if (Page.IsPostBack == true)
        {
            if (RadioFile.Checked == true)  //文件
            {
                strStatus.Text = "<font color=red>必须要有上传的文件.</font>";
            }
            else  //文件夹
            {
                if (FindFolder.HasFile)  //判断浏览文本框中有没有文件内容
                {
                    string stemp2 = Directory.GetParent(FindFolder.PostedFile.FileName).FullName;
                    if (Directory.Exists(stemp2) == true)
                    {
                        //System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(stemp2);
                        //System.IO.FileInfo[] files = directory.GetFiles("*.gif");

                        string[] stemp = Directory.GetFiles(stemp2);
                        int iFile = stemp.Length;
                        for (int i = 0; i < iFile; i++)
                        {
                            Boolean fileOK = false;
                            String fileExtension = System.IO.Path.GetExtension(stemp[i].ToString()).ToLower();
                            String[] allowedExtensions = sFileExtension.Split(',');
                            for (int j = 0; j < allowedExtensions.Length; j++)
                            {
                                string stempExtension = "." + allowedExtensions[j].ToLower();
                                if (fileExtension == stempExtension)
                                {
                                    fileOK = true;
                                    break;
                                }
                            }

                            if (fileOK)
                            {
                                string sTempFileName = System.IO.Path.GetFileName(stemp[i].ToString());  //文件名称

                                //判断该文档是否已归档
                                int iback = MyDataFile.GetCountByqueryStr(" FolderID='" + sFolderID + "' and name = '" + sTempFileName + "' and Status='1' ");
                                if (iback > 0)  //文档已归档,不允许上传到该文件夹
                                {
                                    //sIsDocument = sIsDocument + sTempFileName + "<br>";
                                    ViewState["sIsDocument"] = ViewState["sIsDocument"].ToString().Trim() + stemp[i].ToString() + "<br>";
                                }
                                else  //不存在,或者当前文档没有归档,如果同意上传,则将覆盖掉以前得重名文件
                                {
                                    //判断当前文件夹下面是否存在该文件
                                    int iback2 = MyDataFile.GetCountByqueryStr(" FolderID='" + sFolderID + "' and name = '" + sTempFileName + "'  and Status<>'1' ");
                                    if (iback2 > 0)
                                    {
                                        //sIsFile = sIsFile + sTempFileName + "<br>"; //";
                                        ViewState["sIsFile"] = ViewState["sIsFile"].ToString().Trim() + stemp[i].ToString() + "<br>";
                                    }
                                    hif.Add(stemp[i].ToString());//把文件完整路径添加到数组中去
                                    FileList.Items.Add(stemp[i].ToString()); //获取一个待上传的文件完整路径
                                }
                            }
                            else
                            {
                                errorFile = errorFile + fileExtension;
                            }
                        }
                        string sLeave = "";
                        if (ViewState["sIsDocument"].ToString().Trim() != "")
                        {
                            sLeave = sLeave + "<font color=red>" + ViewState["sIsDocument"] + "</font>文档已归档，不允许上传！<br>";
                        }
                        if (ViewState["sIsFile"].ToString().Trim() != "")
                        {
                            sLeave = sLeave + "<font color=red>" + ViewState["sIsFile"] + "</font>以上文档已存在，是否删除？如果不删除，系统将命名其为最新版本！<br>";
                        }
                        sMessage = sLeave;

                        if (errorFile != "")
                        {
                            strStatus.Text = "您上传的部分文件格式<font color=red>" + errorFile + "</font>不正确,系统只允许上传:" + sFileExtension + "格式文件！";
                        }
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件上传路径错误！');;window.close();</script>");
                        return;
                    }
                }
                else
                {
                    strStatus.Text = "<font color=red>必须要有上传的文件夹.</font>";
                }
            }
            KeepStyle();
        }
        else
        { }
    }


    /// <summary>
    /// 上传批量文档集合
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        string m_Root = ConfigurationManager.AppSettings["DocumentMange"];  //取Web.Config文件中的配置信息,在配置的时候要注意该盘有写权限
        string sPaterID = HID.Value.ToString().Trim();           //父ID 
        string sPaterPath = HPath.Value.ToString().Trim();       //父目录

        string sKeyword = TextAreaKeyword.Value.ToString().Trim();
        string sDorder = this.Dorder.Value.ToString().Trim() == null ? "0" : this.Dorder.Value.ToString().Trim();
        string sMachine = ddlMachineid.SelectedValue.ToString().Trim(); //机房
        string sNetwork = ddlnetwork.SelectedValue.ToString().Trim(); //网元
        string sRegionid = ddlRegionid.SelectedValue.ToString().Trim();  //区域
        string sProject = ddlProject.SelectedValue.ToString().Trim();   //项目

        int ifile = hif.Count;
        int icount = 0;

        if ((FileList.Items.Count == 0) && (filesUploaded == 0)) //judge the listbox'item is nothing
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择需要上传的文件！');</script>");
            return;
        }
        else
        {
            for (int i = 0; i < ifile; i++)
            {
                try
                {
                    string fileName, fileExtension, InFile, OutFile, SwitchPath;

                    fileName = System.IO.Path.GetFileName(hif[i].ToString().Trim()); //文件路径
                    fileExtension = System.IO.Path.GetExtension(fileName);  //文件扩展名      

                    string sExtension = "";
                    string[] aExtension = fileExtension.Split('.');
                    if (aExtension[1].ToString().Trim() != "")
                    {
                        sExtension = aExtension[1].ToString().Trim().ToLower ();
                    }

                    string sRndCode = NetCommClass.GetRandomCode(8);             //系统自动生成指定数目的数字
                    string sTempFileName = sRndCode + fileExtension;
                    
                    InFile = m_Root + fileName + ".Encrypt";

                    SwitchPath = m_Root + HPath.Value + "\\" + sTempFileName;

                    string sSaveFile = m_Root + HPath.Value + "\\" + fileName;


                    string path = sPaterPath + "\\" + fileName; //当前的父路径

                    DataFile datafile = new DataFile();
                    //保存到数据库中
                    FileInfo myfile = new FileInfo(hif[i].ToString());
                    string filelength = myfile.Length.ToString();   //得到文件大小
                    string filetype = System.IO.Path.GetExtension(hif[i].ToString());
                    WebClient MyClient = new WebClient();
                    string s1 = ConfigurationManager.AppSettings["InceptUpFilePath"];  //接收文件路径

                    string shifile = HttpUtility.UrlEncode(hif[i].ToString(), System.Text.Encoding.GetEncoding("GB2312"));
                    //string sddd = HttpUtility.UrlDecode(shifile, System.Text.Encoding.GetEncoding("GB2312"));
                    byte[] sss = MyClient.UploadFile(s1, "POST", hif[i].ToString());   //上传文件到指定的位置
                    //MyClient .UploadFileAsync(        ddd,sss,fff,
                    //string stemo = System.Text.Encoding.Default.GetString(sss);


                    //加密
                    OutFile = InFile.Remove(InFile.Trim().Length - 8, 8);
                    string sEncryptFile = ConfigurationManager.AppSettings["EncryptFile"];

                    BaimasoftFile.EncryptFile(InFile, OutFile, sEncryptFile);//对文件进行加密处理
                    BaimasoftFile.DeleteFile(InFile);            //删除临时文件
                    BaimasoftFile.DeleteFile(SwitchPath);        //删除目的地的已有的文件
                    BaimasoftFile.MoveFile(OutFile, SwitchPath); //从中间层转移到上传层

                    //删除同一文件夹中,重复的文件(实现覆盖原文件的功能)
                    MyDataFile.DeleteDataFileInfo(" name = '" + fileName + "' and FolderID = '" + sPaterID + "' ");
                    //插入文档信息到数据库中
                    MyDataFile.InsDataFileInfo(sPaterID, fileName, sTempFileName, sKeyword, filelength, filetype, sExtension, sDorder, "0", sRegionid, sMachine, sNetwork, sProject, Session["Baimasoft_User_LogonID"].ToString(), DateTime.Now.ToString().Trim(), "0", "", DateTime.Now.ToString().Trim(), "");

                    //日志
                    try
                    {
                        string sPerID = (string)Session["Baimasoft_Pers_ID"];
                        string sContent = "用户" + (string)Session["Baimasoft_User_LogonID"] + "上传了文件：" + sSaveFile;
                        string sIP = Page.Request.UserHostAddress;
                        MyLog.InsertLog("3", sPerID, sContent, sIP);
                    }
                    catch
                    {
                        string sPerID = "0";
                        string sContent = "上传了文件：" + sSaveFile;
                        string sIP = Page.Request.UserHostAddress;
                        MyLog.InsertLog("3", sPerID, sContent, sIP);
                    }


                    icount++;

                }
                catch
                {
                }
            }
            string sAlter = "";
            int ileave = ifile - icount;
            if (ifile == icount)
            {
                sAlter = "文档上传成功！";
            }
            else if (ifile > icount && icount > 0)
            {
                sAlter = "上传成功" + icount + "文档，还有" + ileave + "文档上传失败！";
            }
            else
            {
                sAlter = "上传文档失败！";
            }
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + sAlter + "');window.close();</script>");
            return;
        }
    }


}